package com.hdu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdu.dto.Login;
import com.hdu.entity.Account;
import com.hdu.entity.Menu;
import com.hdu.entity.Student;
import com.hdu.entity.Teacher;
import com.hdu.mapper.*;
import com.hdu.pojo.PageBean;
import com.hdu.service.IAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 用户实体类 服务实现类
 * </p>
 *
 * @author xiaobai
 * @since 2024-05-20
 */
@Service
@RequiredArgsConstructor
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements IAccountService {

    private final AccountMapper accountMapper;

    private final TeacherMapper teacherMapper;

    private final StudentMapper studentMapper;

    private final RoleMapper roleMapper;





    /**
     * 获取用户信息
     */
    @Override
    public PageBean<Account> page(Integer current, Integer size, String nickname) {

        Page<Account> page = new Page<>(current, size);

        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.like("nickname", nickname).likeRight("id", "54");
        IPage<Account> iPage = accountMapper.selectPage(page, wrapper);
        List<Account> accountList = iPage.getRecords();
        for (Account account : accountList) {
            Teacher teacher = teacherMapper.selectById(account.getId());
            if (teacher != null) {
                account.setUserInfo(teacher);
            }
            String[] roleList = account.getRole().split(",");
            List<String> roles = new ArrayList<>();
            for (String role : roleList) {
                roles.add(roleMapper.selectById(role).getRoleName());
            }
            account.setRole(roles.toString().substring(1, roles.toString().length() - 1));
        }
        return new PageBean<>(iPage.getTotal(), accountList);
    }

    /**
     * 新增用户
     */
    @Override
    public void append(Account account) {
        String id = "5" + (int) (Math.random() * 9000000 + 1000000);
        while (accountMapper.selectById(id) != null) {
            id = "5" + (int) (Math.random() * 9000000 + 1000000);
        }
        account.setId(id);
        accountMapper.insert(account);
    }

    /**
     * 编辑用户
     */
    @Override
    public void update(Account account) {
        account.setUpdateTime(LocalDateTime.now());
        Teacher teacher = account.getUserInfo();
        if (teacher != null) {
            teacher.setId(account.getId());
            teacherMapper.updateById(teacher);
        }
        accountMapper.updateById(account);
    }

    /**
     * 分配角色
     */
    @Override
    public void assign(String userId, String[] roleId) {
        UpdateWrapper<Account> wrapper = new UpdateWrapper<>();
        String roleList = Arrays.asList(roleId).toString();
        wrapper.set("role", roleList.substring(1, roleList.length() - 1)).eq("id", userId);
        accountMapper.update(null, wrapper);
    }
}
